package com.zhaoyu.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaoyu.admin.mapper.AdSensitiveMapper;
import com.zhaoyu.admin.service.AdSensitiveService;
import com.zhaoyu.model.admin.dtos.SensitiveDto;
import com.zhaoyu.model.admin.pojos.AdSensitive;
import com.zhaoyu.model.common.dtos.PageResponseResult;
import com.zhaoyu.model.common.dtos.ResponseResult;
import com.zhaoyu.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * IService 实现类（泛型：M 是 mapper 对象，T 是实体）
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult findByNameAndPage(SensitiveDto sensitiveDto) {
        /**
         * 请求参数是null，默认查第一页
         */
        if (sensitiveDto == null){
            sensitiveDto = new SensitiveDto();
            // return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /**
         * 获取默认的分页信息
         */
        sensitiveDto.checkParam();
        /**
         * 分页+模糊查询
         * like(column,val)
         * column 字段(实体类) val 值(DTO)
         */
        Page page = new Page<AdSensitive>(sensitiveDto.getPage(),sensitiveDto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(sensitiveDto.getName())){ // 校验参数 isBlank(null和"") isEmpty(null)
            wrapper.like(AdSensitive::getSensitives,sensitiveDto.getName()); // AdChannel::getSensitives：敏感词名称
        }
        IPage result = page(page,wrapper);
        /**
         * 当前页(默认第一页)：getCurrent
         * 当前页总页数：getSize
         * 总条数：getTotal
         * 分页对象记录列表：getRecords
         * 封装结果
         */
        ResponseResult responseResult = new PageResponseResult((int) result.getCurrent(), (int) result.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        /**
         * 1、检查参数
         */
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /**
         * 2、保存
         */
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult edit(AdSensitive adSensitive) {
        /**
         * 1、检查参数
         */
        if (adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        /**
         * 2、修改
         */
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        /**
         * 1、检查参数
         */
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /**
         * 2、判断当前频道是否存在 和 是否有效
         */
        AdSensitive adSensitive = getById(id);
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        /**
         * 3、删除
         */
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}